<?

class CVS {

  var $data;
  var $newline = "newline";
  var $order = array();
  var $file;

  function CVS($path = "cvs.txt"){
    $this->file = $path;
  }

  function add($field, $value){
    $value = trim($value);
    //$value = str_replace(",","\t",$value);
    $value = str_replace(","," ",$value);
    $value = str_replace("\r\n"," ",$value);
    $this->data->$field = $value;
  }

  function setOutput($path){
    $this->file = $path;
  }

  function writeCVSFile($haveHeader = true){

    if(file_exists($this->file)){
      $line = "";
    }else{
      $line = "";
      // make header from array order
      if($haveHeader){
        foreach($this->order as $d) {
          if($d == $this->newline){
            $line .= "\n";
          }else{
            $line .= $d."\t";
          }
        }
        $line = substr($line,0,-1);
        $line .= "\n";
      }
    }

    $fp=fopen($this->file,"a");

    foreach($this->order as $d) {
      if($d == $this->newline){
        $line .= "\n";
      }else{
        $line .= $this->data->$d."\t";
      }
//    echo $d ." = " . $this->data->$d . "<br>";
    }

    $line = substr($line,0,-1);
    $line .= "\n";

    fwrite($fp,$line);
    fclose($fp);
  }

  function setOrder($order){
    $this->order = $order;
  }

  function clearData(){
    unset($this->data);
  }
}

?>